package towerDefense.circlesprites;

import java.awt.geom.Point2D;
import com.golden.gamedev.object.collision.CollisionRect;
import com.golden.gamedev.object.collision.CollisionShape;


public class CollisionCircle extends Circle implements CollisionShape
{

    public CollisionCircle (Point2D center, double radius)
    {
        super(center, radius);
    }


    public CollisionCircle (Circle c)
    {
        super(c);
    }


    public CollisionCircle ()
    {
        super();
    }


    @Override
    public boolean intersects (CollisionShape shape)
    {
        if (shape instanceof CollisionCircle)
        {
            return this.intersects((CollisionCircle) shape);
        }
        else if (shape instanceof CollisionRect)
        {
            return this.intersects((CollisionRect) shape);
        }
        return false;
    }


    public boolean intersects (CollisionCircle shape)
    {
        return this.intersects(shape);

    }


    public boolean intersects (CollisionRect shape)
    {
        for (Point2D p : super.getRectCorners(shape.getX(),
                                              shape.getY(),
                                              shape.getWidth(),
                                              shape.getHeight()))
            if (this.contains(p)) return true;
        return false;

    }


    @Override
    public void setLocation (double x, double y)
    {
        if (center == null) center = new Point2D.Double(x, y);
        this.center.setLocation(x, y);
    }


    @Override
    public void move (double dx, double dy)
    {
        this.center.setLocation(getX() + dx, getY() + dy);

    }


    /**
     * defines boubs by the best circle approximation to the given rectangle
     */
    @Override
    public void setBounds (double x1, double y1, int w1, int h1)
    {
        this.setBounds(x1 - w1 / 2, y1 - h1 / 2, w1 + h1 / 2);

    }


    public void setBounds (double x1, double y1, double r)
    {
        this.setLocation(x1, y1);
        this.setRadius(r);

    }


    @Override
    public double getX ()
    {
        return center.getX();
    }


    @Override
    public double getY ()
    {
        return center.getY();
    }


    @Override
    public int getWidth ()
    {
        return (int) radius;
    }


    @Override
    public int getHeight ()
    {
        return (int) radius;
    }

}
